﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;
using Newtonsoft.Json;
using System.Web.UI;

namespace Sitefinity.Contrib.SocialWidgets.Shared
{
    public class ImageRotatorUtility
    {
        const int BUTTON_WIDTH = 24;

        private string _prefix = string.Empty;

        /// <summary>
        /// Constructs an ImageRotatorUtility that will use the given prefix on its *ImageRotator* element ids
        /// </summary>
        /// <param name="prefix">The prefix to use. Example: given "Facebook" will generate elements with ids like "FacebookImageRotator"</param>
        public ImageRotatorUtility(string prefix)
        {
            _prefix = prefix;
        }

        public string GenerateScript(string images, int rotationTimerMs)
        {
            // Beware of { and } in string.Format formats. They are escaped by doubling, not by \s
            string optionsFormat = "{{'images':{0},'rotationTimerMs':{1}}}";
            string options = string.Format(optionsFormat, images,  rotationTimerMs);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<script type='text/javascript' language='javascript'>$(document).ready(function(){{$('#{0}SocialRotator').imageRotator({1});$('#{0}SocialRotator').find('a').prettyPhoto();}});</script>", _prefix, options);

            return sb.ToString();
        }

        /// <summary>
        /// Generates the HTML string for the ImageRotator widget container divs
        /// </summary>
        /// <returns>string</returns>
        public string GenerateHtml(int width, int height, string initialHref, string initialSrc, string initialAlt)
        {

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<table style='width:{0}px;height:{1}px;padding:0;'><tr>", width, height);
            sb.AppendFormat("<td id='{0}SocialRotator' style='padding:0;margin:0;'>", _prefix);

            sb.AppendFormat("<a href='{0}' class='imageRotator' rel='prettyPhoto'><img class='imageRotator' src='{1}' alt='{2}' style='max-width:{3}px;max-height:{4}px;",
                initialHref, initialSrc, initialAlt, width, height);
            sb.AppendFormat("width:expression(this.width>{0}px?{0}px:true);height:expression(this.height>{1}px?{1}px:true)' /></a>", width, height);

            sb.Append("</td></tr></table>");

            return sb.ToString();
        }

        /// <summary>
        /// Adds the plugin script references.
        /// </summary>
        /// <param name="page">Page</param>
        public void AddReferences(Page page)
        {
            // We use RegisterStartupScript to put it at the bottom so that Telerik doesn't reload jQuery on us and remove the plugins
            // See http://www.sitefinity.com/devnet/forums/sitefinity-4-x/general-discussions/jquery-plugins-and-or-authoring-custom-jquery-functions-not-supported.aspx

            string INCLUDE_FORMAT = "<script type='text/javascript' language='javascript' src='{0}'></script>";

            page.ClientScript.RegisterStartupScript(this.GetType(), "prettyPhoto_js", 
                string.Format(INCLUDE_FORMAT, page.ResolveUrl("/prettyphoto/js/jQuery.prettyPhoto.js")), false);
            page.ClientScript.RegisterStartupScript(this.GetType(), "imageRotator_js",
                string.Format(INCLUDE_FORMAT, page.ResolveUrl("/imageRotator/jQuery.imageRotator.js")), false);
        }
    }
}